// distributor/pages/applyOrderDetail/applyOrderDetail.js
var common = require("../../../common/common.js").common;

var app = getApp();

Page({
    /**
   * 页面的初始数据
   */
    data: {
        chooseClass: 0,
        page: 1,
        member_id: "",
        order_choice: 1,
        key_word: ""
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        wx.hideShareMenu();
        this.setData({
            clerk: wx.getStorageSync("clerk"),
            domain: app.globalData.domain.slice(0, -10)
        });
        let that = this;
        that.getLevelDetails();
        that.getBase();
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {
        let that = this;
        that.setData({
            page: 1,
            details: []
        });
        that.getLevelDetails();
        that.getBase();
    },
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {
        let that = this;
        that.getLevelDetails();
    },
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {},
    chooseOrder() {
        let that = this;
        console.log(that.data.order_choice);
        that.setData({
            order_choice: that.data.order_choice == 1 ? 2 : 1,
            page: 1,
            details: []
        });
        that.getLevelDetails();
    },
    getBase: function() {
        var that = this;
        common.request({
            endpoint: "set/app_base",
            data: {},
            success: res => {
                that.setData({
                    level_set: res.extra.level_set
                });
                wx.setNavigationBarTitle({
                    title: that.data.level_set.custom_title + "佣金订单明细"
                });
            }
        });
    },
    bindKeyInput(e) {
        let that = this;
        that.setData({
            key_word: e.detail.value
        });
        that.getLevelDetails("keyinput");
    },
    getLevelDetails: function(x) {
        let that = this;
        common.request({
            endpoint: "Level/level/level-order",
            method: "post",
            data: {
                page: that.data.page,
                status: that.data.chooseClass,
                method: that.data.order_choice,
                key_word: that.data.key_word
            },
            success: res => {
                var details = res.extra;
                details.map(x => {
                    x.allowImage = 0;
                    x.order_info.order_money = (Number(x.order_info.esb_delivery) + Number(x.order_info.express_delivery) + Number(x.order_info.leader_delivery) + Number(x.order_info.money)).toFixed(2);
                    x.order_confirm = 0;
                    //确认
                                        x.order_apply = 1;
                    //取消订单
                                        x.delivery_btn = 0;
                    //物流按钮
                                        x.pick_order = 0;
                    //是否是换货订单
                                        x.v.map(item => {
                        if (item.order_status == 4) {
                            //待取货
                            x.order_confirm = 1;
                        }
                        if (item.order_status != 2) {
                            //待发货
                            x.order_apply = 0;
                        }
                        if (item.channel == 5) {
                            //换货订单
                            x.pick_order = 1;
                        }
                        if (item.delivery_channel == 3 || item.delivery_channel == 4) {
                            x.delivery_btn = 1;
                        }
                    });
                });
                //表示输入查询
                                if (x == "keyinput") {
                    that.setData({
                        details: details,
                        page: 1
                    });
                } else {
                    if (details.length > 0) {
                        details = that.data.page == 1 ? details : that.data.details.concat(details);
                        that.setData({
                            details: details,
                            page: that.data.page + 1
                        });
                    }
                }
            }
        });
    },
    allow: function(e) {
        let that = this;
        let tradeNo = e.currentTarget.dataset.trade_no;
        let details = that.data.details;
        details.map(x => {
            if (x.order_info.trade_no == tradeNo) {
                x.allowImage = x.allowImage == 0 ? 1 : 0;
            }
        });
        that.setData({
            details: details
        });
    },
    chooseClass: function(e) {
        let that = this;
        let type = e.currentTarget.dataset.type;
        that.setData({
            chooseClass: type,
            page: 1,
            details: []
        });
        that.getLevelDetails();
    },
    onPageTap: function(e) {
        wx.navigateTo({
            url: e.currentTarget.dataset.url
        });
    },
    cancelOrder: function(e) {
        wx.showModal({
            cancelText: "部分退款",
            confirmText: "整单退款",
            content: "是否整单退款，是否部分退款",
            showCancel: true,
            title: "提示",
            success: result => {
                if (res.confirm) {} else {
                    wx.navigateTo({
                        url: e.currentTarget.dataset.url
                    });
                }
            },
            fail: res => {},
            complete: res => {}
        });
    },
    confirm_all: function(e) {
        var that = this;
        var clerk = that.data.clerk;
        var tradeNo = e.currentTarget.dataset.tradeno;
        var openid = "";
        if (clerk.wom_status == 0 || clerk.wom_status == undefined) {
            openid = wx.getStorageSync("user").openid;
        } else {
            openid = clerk.leader_openid;
        }
        var member_id = that.data.member_id;
        var details = that.data.details;
        var item_id = [];
        details.map(item => {
            if (item.order_info.trade_no == tradeNo) {
                item.v.map(x => {
                    item_id.push(x.item_id);
                });
            }
        });
        console.log("item_id", item_id);
        wx.showModal({
            title: "提示",
            content: "是否一键确认取货！",
            success: function(res) {
                if (res.cancel) {} else {
                    common.request({
                        endpoint: "My/waitpick/pickorder_all",
                        method: "post",
                        data: {
                            user_id: member_id,
                            openid: openid,
                            item_id: item_id
                        },
                        success: res => {
                            wx.showToast({
                                title: "取货成功",
                                icon: "success"
                            });
                            details.map(item => {
                                if (item.order_info.trade_no == tradeNo) {
                                    item.order_confirm = 0;
                                }
                            });
                            that.setData({
                                details: details
                            });
                        },
                        fail(err) {
                            wx.showModal({
                                title: "提示",
                                content: err.message,
                                showCancel: false
                            });
                        }
                    });
                }
            }
        });
    }
});